/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.task.dao;

import java.util.*;

import javacommon.base.*;
import javacommon.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import com.tech15.task.model.*;
import com.tech15.task.dao.*;
import com.tech15.task.service.*;
import com.tech15.task.vo.query.*;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */


import static cn.org.rapid_framework.util.ObjectUtils.*;
import org.springframework.stereotype.Repository;

@Repository
public class TaskReturnDao extends BaseHibernateDao<TaskReturn,Long>{

	public Class getEntityClass() {
		return TaskReturn.class;
	}
	
	public Page findByPageRequest(PageRequest<Map> pageRequest) {
		//XsqlBuilder syntax,please see http://code.google.com/p/rapid-xsqlbuilder
		// [column]为字符串拼接, {column}为使用占位符. 以下为图方便采用sql拼接,适用性能要求不高的应用,使用占位符方式可以优化性能. 
		// [column] 为PageRequest.getFilters()中的key
		String sql = "select t from TaskReturn t where 1=1 "
				+ "/~ and t.taskid = '[taskid]' ~/"
				+ "/~ and t.tpid = '[tpid]' ~/"
				+ "/~ and t.thr = '[thr]' ~/"
				+ "/~ and t.ssly = '[ssly]' ~/"
				+ "/~ and t.sssj = '[sssj]' ~/"
				+ "/~ and t.ssjg = '[ssjg]' ~/"
				+ "/~ and t.zt = '[zt]' ~/"
				+ "/~ and t.ssr = '[ssr]' ~/"
				+ "/~ order by [sortColumns] ~/";
		return pageQuery(sql,pageRequest);
	}
	
	public Page findPage(TaskReturnQuery query) {
		String sql = "select t.*,t1.title,t2.realname as ssrxm,t3.realname from Task_Return t " +
				" left join task t1 on t.taskid=t1.taskid " +
				" left join sys_user t2 on t1.creater=t2.userid " +
				" left join sys_user t3 on t.thr=t3.userid where 1=1 "
			+ "/~ and t.taskid = '[taskid]' ~/"
			+ "/~ and t.tpid = '[tpid]' ~/"
			+ "/~ and t.thr = '[thr]' ~/"
			+ "/~ and t.ssly = '[ssly]' ~/"
			+ "/~ and t.sssj = '[sssj]' ~/"
			+ "/~ and t.ssjg = '[ssjg]' ~/"
			+ "/~ and t.zt = '[zt]' ~/"
			+ "/~ and t.ssr = '[ssr]' ~/"
			+ "/~ order by [sortColumns] ~/";
		
		Map map = new HashMap();
		map.put("trid", "Long");
		map.put("taskid", "Long");
		map.put("tpid", "Long");
		map.put("thr", "Long");
		map.put("title", "String");
		map.put("realname", "String");
		map.put("ssly", "String");
		map.put("ssjg", "String");
		map.put("sssj", "Timestamp");
		map.put("thsj", "Timestamp");
		map.put("zt", "Integer");
		map.put("ssr", "Long");
		map.put("ssrxm", "String");
		
		return pageSqlQuery(sql, query, map, TaskReturn.class);
	}
	
	public Page my(TaskReturnQuery query) {
		String sql = "select c.* from ("+
				"select t.*,t1.title,t2.realname as zrrxm,t3.realname from Task_Return t " +
				" left join task t1 on t.taskid=t1.taskid " +
				" left join sys_user t2 on t1.creater=t2.userid " +
				" left join sys_user t3 on t.thr=t3.userid where 1=1 ";
				
			if(isNotEmpty(query.getZrr())){
				sql += "and ( (t1.creater="+query.getZrr()+") or (t.curclr="+query.getZrr()+" and (t.zt<>1 and t.zt<>2)) )";
			}
			
			sql += " union ";
			
			sql += "select t.*,t1.title,t2.realname as zrrxm,t3.realname from Task_Return t " +
			" left join task t1 on t.taskid=t1.taskid " +
			" left join sys_user t2 on t1.creater=t2.userid " +
			" left join sys_user t3 on t.thr=t3.userid where 1=1 ";
			
			if(isNotEmpty(query.getZrr())){
				sql += "and t.zt=3 and (select count(*) from task_return_process a where a.trid=t.trid and a.userid="+query.getZrr()+" and a.zt=0) >0";
			}
			
			sql += ") c order by c.thsj desc ";
			
			
			/*sql += " order by t.thsj desc ";
			+ "/~ and t1.creater = [zrr] ~/"
			+ "/~ and t.bz = [zrr] ~/"
			+ "/~ and t.gg = [zrr] ~/"
			+ "/~ and t.taskid = '[taskid]' ~/"
			+ "/~ and t.tpid = '[tpid]' ~/"
			+ "/~ and t.thr = '[thr]' ~/"
			+ "/~ and t.ssly = '[ssly]' ~/"
			+ "/~ and t.sssj = '[sssj]' ~/"
			+ "/~ and t.ssjg = '[ssjg]' ~/"
			+ "/~ and t.zt = '[zt]' ~/"
			+ "/~ and t.ssr = '[ssr]' ~/"
			+ "/~ order by [sortColumns] ~/";*/
		
		Map map = new HashMap();
		map.put("trid", "Long");
		map.put("taskid", "Long");
		map.put("tpid", "Long");
		map.put("thr", "Long");
		map.put("title", "String");
		map.put("realname", "String");
		map.put("ssly", "String");
		map.put("ssjg", "String");
		map.put("sssj", "Timestamp");
		map.put("thsj", "Timestamp");
		map.put("zt", "Integer");
		map.put("ssr", "Long");
		
		return pageSqlQuery(sql, query, map, TaskReturn.class);
	}

	public Integer myReturnCount(Long userid) {
		String sql = "select count(*) from Task_Return t left join task t1 on t.taskid=t1.taskid where " +
				" (t.zt=-1 and t1.creater="+userid+") or " +
				" (t.curclr="+userid+" and (t.zt<>1 and t.zt<>2) ) or " +
				" (t.zt=3 and (select count(*) from task_return_process a where a.trid=t.trid and a.userid="+userid+" and a.zt=0) >0)";
		return this.getCount(sql);
	}
	
	public List<TaskReturn> searchTimeout(){
		String sql = "select t from TaskReturn t where t.bz is not null and t.gg is null and (t.zt=-2 or t.zt=0) and (sysdate-t.gbzsj)>3";
		return getHibernateTemplate().find(sql);
	}
	

}
